package com.sicheng.algorithm.array.sorted;

import org.junit.Test;

import java.util.Arrays;

import static com.sicheng.algorithm.common.Sort.less;
import static com.sicheng.algorithm.common.Sort.swap;

/**
 * @author zsc
 * @version 1.0
 * @date 2021/9/19 20:40
 */

public class InsertSort {

    public void sort(Integer[] nums) {
        if (nums == null || nums.length <= 1) {
            return;
        }
        int N = nums.length;
        for (int i = 1; i < N; i++) {
            for (int j = i; j > 0 && less(nums[j], nums[j - 1]); j--) {
                swap(nums, j, j - 1);
            }
        }
    }

    @Test
    public void insertSortTest() {
        Integer[] nums = {1, 0, 9, 3, 0, -2, 8};
        sort(nums);
        System.out.println(Arrays.toString(nums));
    }
}

